import java.io.File;

import net.zxteam.solutions.java.rs_sync.RSSync;

public class Main {

	public static void main(String[] args) {

		if(args != null && args.length > 1) {

			String cmd = args[0];
			if("init".equals(cmd)) { // init

				if(args.length > 2) {

					try {

						String sourceRep = args[1];
						long sourceInitRev = 0;
						String destinationRep = args[2];

						RSSync ssc = RSSync.newInstance();

						String SourceRepositoryAuthUser = null, SourceRepositoryAuthPassword = null,
							DestinationRepositoryAuthUser = null, DestinationRepositoryAuthPassword = null,
							TempDir = null;

						if(args.length > 3) {

							for(int argIndex = 3; argIndex < args.length; argIndex++) {

								if("-r".equals(args[argIndex])) {

									String initRevStr = args[++argIndex];
									sourceInitRev = Long.parseLong(initRevStr);
								}
								else if("-du".equals(args[argIndex])) {

									DestinationRepositoryAuthUser = args[++argIndex];
								}
								else if("-dp".equals(args[argIndex])) {

									DestinationRepositoryAuthPassword = args[++argIndex];
								}
								else if("-su".equals(args[argIndex])) {

									SourceRepositoryAuthUser = args[++argIndex];
								}
								else if("-sp".equals(args[argIndex])) {

									SourceRepositoryAuthPassword = args[++argIndex];
								}
								else if("--temp-dir".equals(args[argIndex])) {

									TempDir = args[++argIndex];
								}
								else
									throw new IllegalArgumentException("Unsupported parameter: " + args[argIndex]);
							}
						}

						ssc.setSourceAuthentication(SourceRepositoryAuthUser, SourceRepositoryAuthPassword);
						ssc.setDestinationAuthentication(DestinationRepositoryAuthUser, DestinationRepositoryAuthPassword);
						ssc.setDestinationRepositoryUrl(destinationRep);
						if(TempDir!=null) ssc.setTempDirectory(new File(TempDir));
						ssc.init(sourceRep, sourceInitRev);

					} catch (Exception e1) {

						e1.printStackTrace();
					}

					return;
				}
				else
					throw new IllegalArgumentException("Bad parameters for 'init' command");
			}
			else if("sync".equals(cmd)) { // sync
				
				String destinationRep = args[1];

				RSSync ssc = RSSync.newInstance();

				try {

					String SourceRepositoryAuthUser = null, SourceRepositoryAuthPassword = null,
						DestinationRepositoryAuthUser = null, DestinationRepositoryAuthPassword = null,
						TempDir = null;

					if(args.length > 2) {

						for(int argIndex = 2; argIndex < args.length; argIndex++) {
	
							if("-du".equals(args[argIndex])) {

								DestinationRepositoryAuthUser = args[++argIndex];
							}
							else if("-dp".equals(args[argIndex])) {

								DestinationRepositoryAuthPassword = args[++argIndex];
							}
							else if("-su".equals(args[argIndex])) {

								SourceRepositoryAuthUser = args[++argIndex];
							}
							else if("-sp".equals(args[argIndex])) {

								SourceRepositoryAuthPassword = args[++argIndex];
							}
							else if("--temp-dir".equals(args[argIndex])) {

								TempDir = args[++argIndex];
							}
							else
								throw new IllegalArgumentException("Unsupported parameter: " + args[argIndex]);
						}
					}

					ssc.setSourceAuthentication(SourceRepositoryAuthUser, SourceRepositoryAuthPassword);
					ssc.setDestinationAuthentication(DestinationRepositoryAuthUser, DestinationRepositoryAuthPassword);
					ssc.setDestinationRepositoryUrl(destinationRep);
					if(TempDir!=null) ssc.setTempDirectory(new File(TempDir));
					ssc.sync();

				} catch (Exception e1) {
					e1.printStackTrace();
				}

				return;
			}
			else
				throw new IllegalArgumentException("Unknown command: " + cmd);
		}

		System.out.println("\r\nUsage of rs-sync:\r\n\r\n" +
				"\trs-sync init <sourceRepositoryUrl> <destinationRepositoryUrl> [-r SourceRepositoryInitRevision = 0] [-su SourceRepositoryAuthUser] [-sp SourceRepositoryAuthPassword] [-du DestinationRepositoryAuthUser] [-dp DestinationRepositoryAuthPassword] [--temp-dir TempDirectory]\r\n\r\n" +
				"\trs-sync sync <repositoryUrl>\r\n\r\n");

		//ssc.setDestinationRepositoryUrl("svn://svn.anurin.org.ua/test/opencart/trunk-owner");
		//ssc.setDestinationRepositoryUrl("file:///home/maxim.anurin/tmp/rep/f3");
		//ssc.setDestinationRepositoryUrl("file:///C:/Users/maxim.anurin/svnserve/Jordan/KPWin2000/trunk");
		//ssc.setDestinationRepositoryUrl("file:///C:/Users/maxim.anurin/svnserve/TempRepos/Test-Jordan");
		//ssc.setDestinationRepositoryUrl("file:///C:/Documents and Settings/Administrator/My Documents/temp/repos/RSSync_Test_1");

		//ssc.init("file:///C:/Users/maxim.anurin/svnserve/TempRepos/depends", 0);
		//ssc.init("file:///C:/Users/maxim.anurin/svnserve/TempRepos/Test3", 0);
		//ssc.init("file:///C:/Users/maxim.anurin/svnserve/TempRepos/test-with-branch", 0);
		//ssc.init("svn://svn.anurin.org.ua/repository/00000025/trunk", 0);

	}
}
